﻿
$.MGJ.NodeConfig = [];

var NodeConfig = $.MGJ.NodeConfig;

(function () {
    var _this = $.MGJ.NodeConfig;

    $.extend($.MGJ.NodeConfig, {

        //批次列表  点击溯源节点配置
        Dialog_NodeConfigObj: null,
        Dialog_NodeConfig: function (batchId) {
            this.Dialog_NodeConfigObj = $.L.dialog({
                url: ACTION_URL("NodeConfig", "OriginsNode") + "?batchId=" + batchId,
                title: "节点配置",
                width: 1100,
                height: 'auto',
                buttons: {
                    //"button_save": {
                    //    type: 'primary',
                    //    text: '保存',
                    //    click: 'MODALMISS'
                    //},
                    "button_cancel": {
                        type: 'default',
                        text: '关闭',
                        click: 'MODALMISS'
                    }
                }
            });

        },

        //根据模板ID获取节点列表
        Ajax_GetTempletNodeList: function (templetId) {
            $.L.ajaxHandle({
                url: ACTION_URL("_GetTempletNodeList", "OriginsNode"),
                data: { templetId: templetId },
                success: function (data) {
                    //console.log(data);
                    $("#NodeData").L_bindingDataList(data);
                }
            })
        },

        //编辑节点数据
        Ajax_EditNode: function (obj) {
            var nodeId = $(obj).attr("NodeId");

            $.L.ajaxHandle({
                url: ACTION_URL("GetNodeModel", "OriginsNode") + "?nodeId=" + nodeId,
                success: function (data) {
                    _this.L_bindingNode(data);
                    for (var i = 0; i < $(".AddContent").length; i++) {
                        for (var n = 0; n < data.Display.length; n++) {
                            if (i == data.Display[n]) {
                                $(".AddContent").eq(i).addClass("green-500")
                            }
                        }
                    }

                }
            })
        },

        Ajax_NodeSave: function (templetId) {
            var chk_value = "";
            $('input[name="DisplayChe"]:checked').each(function () {
                chk_value += $(this).val() + ",";
            });
            $("#Display").val(chk_value.substring(0, chk_value.length - 1));
            var data=$("#Node_form").serialize();
            $.L.ajaxHandle({
                url: ACTION_URL("_NodeSave", "OriginsNode"),
                data: data,
                success: function (ht) {
                    if (ht == 0)
                    {
                        $.L.msgWarning('节点信息不能为空！')
                    }
                    if (ht.result > 0) {
                        _this.Ajax_GetTempletNodeList(templetId);
                        _this.NodeInfoIniti();
                        $("#SourceNodeRight").hide();
                    }
                }
            });

        },
        //点击 添加节点 
        Ajax_AddNode: function () {
            _this.NodeInfoIniti();
            $("#SourceNodeRight").show();
        },
       //删除节点
        Ajax_DeleteNode: function (obj) {

            var nodeId = $(obj).attr("NodeId");
            var templetId = $("#FlowID").val();
            var result = confirm('是否删除！');
            if (result) {
                $.L.ajaxHandle({
                    url: ACTION_URL("NodeDelete", "OriginsNode") + "?nodeId=" + nodeId,
                    success: function (result) {
                        if (result > 0) {
                            _this.Ajax_GetTempletNodeList(templetId);
                        }
                    }
                });
            }
        },
        //点击编辑 绑定数据
        L_bindingNode: function (data) {
            _this.NodeInfoIniti();
            $("#SourceNodeRight").show();
            if (data.Permission == 0) {
                $("#Modifier").hide();
            } else {
                $("#Modifier").show();
            }

            $("#id").val(data.Id);
            $("#NodeName").val(data.NodeName);
            $("#Category").find("option[value='" + data.Category + "']").prop("selected", true);
            $("input[type='radio'][name='EnterTimes'][value='" + data.EnterTimes + "']").prop("checked", true);
            $("input[type='radio'][name='IsH5'][value='" + data.IsH5 + "']").prop("checked", true);
            $("input[type='radio'][name='Permission'][value='" + data.Permission + "']").prop("checked", true);
            $("#UserSelect").val(data.PermissionPerson);


            if (data.Display != "" || data.Display != null) {
                if (data.Display.length > 1) {
                    $(data.Display.split(',')).each(function (i, e) {
                        $("input[type='checkbox'][name='DisplayChe'][value='" + e + "']").prop("checked", "checked");
                    });
                }
                else {
                    $("input[type='checkbox'][name='DisplayChe'][value='" + data.Display + "']").prop("checked", "checked");
                }
            }
        },
        //节点信息初始化
        NodeInfoIniti: function () {
            $("#id").val(0);
            $("#NodeName").val("");
            $("#SourceNodeRight").find("input").prop("checked", false);
            $("#SourceNodeRight").find("select").find("option").eq(0).prop("selected", true);
            $(".AddContent").removeClass("green-500");
        },
        GetUserListByEid: function () {

            $.L.ajaxHandle({
                url: ACTION_URL("GetNodeAddUserList", "Manager"),
                success: function (result) {
                    if (result.length > 0) {
                        _this.BindingUserSelect(result);
                    }
                }
            });

        },

        BindingUserSelect: function (list) {

            var html = "";
            for (var i = 0; i < list.length; i++) {
                html += "<option value='" + list[i].ID + "'>" + list[i].RealName + "</option>"
            }
            $("#UserSelect").html(html);
        },

        //从模板添加
        Dialog_FromTempletObj: null,
        Dialog_FromTemplet: function (templetId) {
            this.Dialog_FromTempletObj = $.L.dialog({
                url: ACTION_URL("FromTemplet", "OriginsNode"),
                title: "从模板添加",
                width: 1100,
                height: 'auto',
                buttons: {
                    "button_save": {
                        type: 'primary',
                        text: '保存',
                        click: function () {
                            _this.Ajax_setTemplete(templetId);
                        }
                    },
                    "button_cancel": {
                        type: 'primary',
                        text: '返回',
                        click: 'MODALMISS'
                    }
                }
            });
        },

        Ajax_setTemplete: function (templetId) {
            var sysTempletId = $("input[name='tdRadio']:checked").val();
            if (sysTempletId == undefined) {
                $.L.msgWarning('请选择模版');
                return; 
            }
         
            $.L.ajaxHandle({
                url: ACTION_URL("SetTemplet", "OriginsNode"),
                data: { sysTempletId: sysTempletId, templetId: templetId },
                success: function (result) {
                    if (result > 0) {
                        if (confirm("操作成功，是否返回上一页面？")) {

                            _this.Dialog_FromTempletObj.close();
                            
                            _this.Ajax_GetTempletNodeList(templetId);
                        }
                    }
                }
            });
        },

        getCategoryName: function (val) {
     
            if (val == 0) {
                return "原料节点";
            } else if (val == 1) {
                return "关键节点"
            } else {
                return "其他"
            }
        },


        getEnterTimes: function (val) {
       
            if (val == 0) {
                return "单次";
            } else if (val == 1) {
                return "多次"
            }
        }




    })

})(jQuery);

